import React from 'react';
import {injectIntl, language} from './intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
const FormItem = Form.Item;
import TextInput from 'Shared/components/TextInput';
import {PERMISSION} from './constants';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {hasPermissions} from './utils';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TagSelect from 'Shared/components/TagSelect';
import {saleOrderStatus} from '../Enum';
import styles from './style.css';
import routes from './routes';
import WrappedSelect from '../common/WrappedSelect';
import Export from '../common/Export';
const ButtonGroup = Button.Group;

class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    };

    render() {
        const operations = [];
        const otherBtns = [];
        if(hasPermissions(this.props.permissions, PERMISSION.import))
            otherBtns.push(
                <Button key={PERMISSION.import} href={`#${routes.import.url()}`}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.import',
                        defaultMessage: '导入'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.export))
            otherBtns.push(<Export key={PERMISSION.export} exportRequest={this.props.onClickExport} />);
        if(hasPermissions(this.props.permissions, PERMISSION.add))
            operations.push(
                <Button key="add" type="primary" href={`#${routes.add.url()}`}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.add',
                        defaultMessage: '新增'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.assistAdd))
            operations.push(
                <Button key="assistAdd" type="primary" href={`#${routes.assistAdd.url()}`}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.assistAdd',
                        defaultMessage: '代新增'
                    })}
                </Button>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.code',
                                    defaultMessage: '订单编号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.type',
                                    defaultMessage: '订单类型'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="typeId"
                                    value={this.props.conditions.typeId}
                                    options={this.props.types}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.createTime',
                                    defaultMessage: '创建时间'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="createTime" value={this.props.conditions.createTime} onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        {this.props.isDealerUser ? null
                            : <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.dealerCode',
                                        defaultMessage: '总代编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="dealerCode"
                                        value={this.props.conditions.dealerCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>}
                    </Row>
                    <Row>
                        <Col>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="status"
                                    value={this.props.conditions.status}
                                    options={saleOrderStatus.toList(language)}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                className={styles.btnMargin}
                                onClick={this.props.querySearch}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            <ButtonGroup>{otherBtns}</ButtonGroup>
                            {operations}
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    isDealerUser: PropTypes.bool.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    types: PropTypes.array.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    intl: PropTypes.object
};
import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, onClickExport} from './actions';
const EMPTY_ARRAY = [];
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getEnterprise = selectorFactory(['page', 'domainData', 'initData', 'enterprise']);
const getQueryData = selectorFactory(['page', 'appState', 'queryCondition']);
const getConditions = createSelector(
    state => getQueryData(state),
    state => state.getIn(['page', 'domainData', 'initData', 'isDealerUser']),
    state => getEnterprise(state),
    (queryData, isDealerUser, enterprise) => {
        if(!isDealerUser)
            return queryData;
        const {code} = enterprise;
        return {
            ...queryData,
            dealerCode: code,
        };
    }
);
const getTypes = createSelector(
    state => state.getIn(['page', 'domainData', 'initData', 'types']),
    data => {
        const types = data.toJS();
        return types
            ? types.map(item => ({
                text: item.typeName,
                value: item.id
            }))
            : EMPTY_ARRAY;
    }
);

const mapStateToProps = state => ({
    conditions: getConditions(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
    isDealerUser: state.getIn(['page', 'domainData', 'initData', 'isDealerUser']),
    types: getTypes(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onClickReset: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(onClickExport()),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(QueryPanel));
